package cn.tedu.eguser.basic.exception;

import cn.tedu.eguser.basic.response.JsonResult;
import cn.tedu.eguser.basic.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GloableExceptionHandle {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doHandleIllegalArgumentException(MethodArgumentNotValidException ex){
        String data =ex.getFieldError().getDefaultMessage();
        log.error("doHandleIllegalArgumentException"+data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doConstraintViolationException(ConstraintViolationException ex){
        String data =ex.getMessage().split(":")[1].trim();
        log.error("doConstraintViolationException:"+data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }

}
